package com.base.system.managedBean;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.Validate;
import org.operamasks.faces.user.util.Browser;

import com.base.common.config.CommonKeyConst;
import com.base.common.config.RepositoryConst;
import com.base.commonKey.entity.CommonKeyType;
import com.base.system.managedBean.baseBean.BaseCommonKeyTypeBean;

@ManagedBean(name = "system_editCommonKeyTypeBean", scope = ManagedBeanScope.REQUEST)
public class EditCommonKeyTypeBean extends BaseCommonKeyTypeBean {

	@Inject
	private FacesContext context;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			Map<String, Object> session = context.getExternalContext()
					.getSessionMap();
			com = (CommonKeyType) session
					.get(CommonKeyConst.COMMONKEYTYPE_SELECT_ROW_DATA);
			this.getProperty();
		}
	}

	@Action(id = "saveCommonKey")
	public void saveCommonKey() {
		com = new CommonKeyType();
		setProperty();
		dao.saveOrUpdate(com);
		String script = "closeCenterLayoutTab('" + CommonKeyConst.EDIT_TAB
				+ "')";
		Browser.execClientScript("window.top", script);
	}

	@Validate(id = "typeCode", message = "输入了非英文字符")
	private boolean validateItemName(String typeCode) {
		Pattern p = Pattern.compile(RepositoryConst.REPOSITORY_REGULAR_ENGLISH);
		Matcher m = p.matcher(typeCode);
		return m.matches();
	}

}
